<template>
  <div>
    <!-- 卡片视图 -->
    <el-card shadow="always">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>运营</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>报销分类</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索框 -->
      <el-input
        style="width:280px;"
        placeholder="请输入报销分类"
        class="search"
        v-model="searchData"
        clearable
      >
        <i slot="prefix" class="el-input__icon el-icon-search"></i>>
        <el-button slot="append" @click="searchBtn" :disabled="isDisabled"
          >搜索</el-button
        >
      </el-input>
      <el-button
        class="top-right-button"
        type="success"
        @click="addClassifcation"
        >添加报销分类</el-button
      >
      <!-- 数据列表 -->
      <el-table :data="tableData" border>
        <el-table-column type="index" label="#" width="75"></el-table-column>
        <el-table-column prop="title" label="报销分类"></el-table-column>
        <el-table-column label="所需字段">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.fieldJson" :key="index">
              <el-tag
                :type="tagColorList[index % 5]"
                @click.stop="tableRowClick(scope.row, index)"
                class="mr10"
                >{{ item.fieldCh }}</el-tag
              >
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="70">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="详情"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="mini"
                type="primary"
                @click.stop="showDetail(scope.row)"
              >
                <i class="el-icon-s-operation" style="margin: 0 auto"></i>
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="p"
        :page-sizes="[10, 20, 50, 80]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      ></el-pagination>
    </el-card>

    <!-- 详情 -->
    <el-dialog
      :title="detailTitle + '详情'"
      :visible.sync="detailDialogVisible"
      width="40%"
      center
    >
      <div>
        <el-table :data="errandInfo" style="width: 100%">
          <el-table-column
            prop="users_name"
            label="代报员姓名"
          ></el-table-column>
          <el-table-column
            prop="users_phone"
            label="联系方式"
          ></el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="detailDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 添加报销分类 -->
    <el-dialog
      title="添加报销分类"
      :visible.sync="addKindDialogVisible"
      width="30%"
      center
      @close="addKindDialogVisibleclose"
    >
      <div class="centerWrap">
        <div>
          报销分类：
          <input
            type="text"
            placeholder="请输入内容"
            class="input_"
            v-model="inputKind"
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addKindDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subKind" :disabled="isSubDisabled"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 报销分类所需字段的说明 -->
    <el-dialog
      title="报销分类所需字段的说明"
      v-if="isShowReviewPageEdit"
      :visible.sync="isShowReviewPageEdit"
      width="600px"
      center
    >
      <el-form @submit.native.prevent.stop="confim(selectValue)">
        <el-form-item label="活动名称" label-width="100px">
          <el-select
            v-model="showText"
            class="myInput"
            placeholder="请选择"
            @change="select"
          >
            <el-option
              v-for="(item, index) in isShowReviewPageEditData.fieldJson"
              :key="index"
              :label="item.fieldCh"
              :value="item.fieldEn"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" label-width="100px">
          <el-input
            class="myInput"
            type="textarea"
            rows="10"
            v-model="selectValue.remark"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="btn_container">
            <el-button type="primary" round native-type="submit"
              >确认</el-button
            >
            <el-button round @click="isShowReviewPageEdit = false"
              >取消</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "../../../../../public/API/query";
import config from "../../../../../public/config";

export default {
  data() {
    return {
      //搜索
      searchData: "",
      //列表数据
      tableData: [],
      errandInfo: [],
      //查询参数
      p: 1,
      limit: 10,
      //总条数
      count: null,
      isDisabled: false,
      detailDialogVisible: false,
      addKindDialogVisible: false,
      inputKind: "",
      isSubDisabled: false,
      detailTitle: "",

      // tag颜色数组
      tagColorList: ["type", "success", "info", "warning", "danger"],
      // 编辑报销dialog
      isShowReviewPageEdit: null,
      isShowReviewPageEditData: null,
      selectValue: "",
      selectText: "",
      // 编辑字段id
      selectId: "",
      showText: "",
    };
  },
  methods: {
    addClassifcation() {
      get("operate/commodity/classification/addPage", {}, (res) => {
        this.addKindDialogVisible = true;
      });
    },
    // 获取列表数据
    getTableData(title) {
      let params = {
        p: this.p,
        limit: this.limit,
      };
      if (title) {
        params.title = title;
      }
      get("operate/commodity/classification/index", params, (res) => {
        if (res.code == 200) {
          this.tableData = res.data._list;
          this.tableData.forEach((item) => {
            item.fieldJson = JSON.parse(item.fieldJson);
          });
          this.count = res.data.count;
        }
      });
    },

    // 点击确认修改事件
    confim(item) {
      const { remark, fieldEn } = item,
        parms = { id: this.selectId, fieldEn: fieldEn, remark: remark };
      get("operate/commodity/classification/EditFieldRemark", parms, (res) => {
        console.log(res);
        if (res.code == 200) {
          this.$message.success("修改成功！");
        } else {
          this.$message.error(res.data);
        }
        this.isShowReviewPageEdit = false;
      });
    },

    handleSizeChange(newSize) {
      this.limit = newSize;
      this.getTableData();
    },
    handleCurrentChange(newPage) {
      this.p = newPage;
      this.getTableData();
    },
    //搜索功能
    searchBtn() {
      this.getTableData(this.searchData);
    },
    // 详情页
    showDetail(row) {
      this.detailTitle = row.title;
      get("operate/commodity/classification/detail", { id: row.id }, (res) => {
        if (res.code == 200) {
          this.detailDialogVisible = true;
          this.errandInfo = res.data.result;
        }
      });
    },
    // 提交分类
    subKind() {
      get(
        "operate/commodity/classification/addSub",
        { title: this.inputKind },
        (res) => {
          if (res.code == 200) {
            this.$message.success("添加报销分类成功！");
            get("operate/commodity/classification/addPage", {}, (res) => {
              if (res.code == 200) {
                this.addKindDialogVisible = false;
                this.getTableData();
              }
            });
          } else {
            this.isSubDisabled = true;
            let num = 3;
            let timer = setInterval(() => {
              num--;
              if (num == 0) {
                this.isSubDisabled = false;
              }
            }, 1000);
          }
        }
      );
    },
    // 添加分类关闭回调
    addKindDialogVisibleclose() {
      this.inputKind = "";
    },
    // 必须字段说明编辑
    tableRowClick(row, index) {
      console.log(row);
      this.isShowReviewPageEdit = true;
      this.isShowReviewPageEditData = row;
      this.selectId = row.id;
      if (index != 0 && !index) {
        this.selectValue = row.fieldJson[0];
      } else {
        this.selectValue = row.fieldJson[index];
      }
      this.showText = this.selectValue.fieldCh;
    },
    // 所选字段改变
    select(item) {
      // this.selectValue = item;
      this.selectValue = this.isShowReviewPageEditData.fieldJson.find(
        (data) => {
          console.log(data);
          if (data.fieldEn == item) {
            return data;
          }
        }
      );
      console.log(this.selectValue);
    },

  },
  computed: {
    getJSON(json) {
      return JSON.parse(json);
    },
  },
  watch: {
    searchData: function() {
      if (!this.searchData) {
        this.getTableData();
      }
      if (this.searchData) {
        this.isDisabled = false;
      }
    },
  },

  created() {
    this.getTableData();
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 30px;
}
.el-input {
  margin-bottom: 30px;
  width: 280px;
  margin-right: 30px;
}
.el-pagination {
  margin-top: 30px;
}
.addRulesWrap {
  margin-left: 80px;
}
.addRules {
  position: absolute;
  right: 25%;
  bottom: 5%;
}
.disable {
  margin-left: 15px;
}
.top-right-button {
  margin-left: 60%;
}
.centerWrap {
  width: 100%;
  display: flex;
  justify-content: center;
}
.mr10 {
  margin-right: 10px;
}
.btn_container {
  width: 200px;
  margin: auto;
}
.myInput {
  width: 400px;
}
</style>
